package zd.cornermemory.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import zd.cornermemory.R;
import zd.cornermemory.db.Cj;
import zd.cornermemory.utils.Statistics;
import zd.cornermemory.view.SwipeMenuLayout;

public class CjAdapter extends RecyclerView.Adapter<CjAdapter.VH> {
	private Context        mContext;
	private List<Cj>       cjs;
	private LayoutInflater mInflater;
	private OnClickItem onClickItem;

	public static class VH extends RecyclerView.ViewHolder {
		public TextView cj,code;
		public RelativeLayout  item_layout;
		public Button          btnDelete;
		public SwipeMenuLayout swipe_all;
		public VH(View v){
			super(v);
			cj = (TextView) v.findViewById(R.id.sj);
			code = (TextView) v.findViewById(R.id.code);
			item_layout = (RelativeLayout) v.findViewById(R.id.item_layout);
			btnDelete = (Button) v.findViewById(R.id.btnDelete);
			swipe_all = (SwipeMenuLayout) v.findViewById(R.id.swipe_all);
		}
	}

	public void setOnClickItem(OnClickItem onClickItem) {
		this.onClickItem = onClickItem;
	}

	public CjAdapter(Context context, List<Cj> cjs) {
		this.mContext = context;
		this.mInflater = LayoutInflater.from(context);
		this.cjs = cjs;
	}

	@Override
	public VH onCreateViewHolder(ViewGroup parent, int viewType) {
		View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cj_item, parent, false);
		return new CjAdapter.VH(v);
	}

	@Override
	public void onBindViewHolder(final VH holder, final int position) {
		Cj cj = cjs.get(position);
		String date = Statistics.handleLongDate(cj.getDate());
		String code = cj.getCode();
		String time = Statistics.timeToString(cj.getSingTime().intValue());
		switch (cj.getStatus()) {
			case 0:
				time = Statistics.timeToString(cj.getSingTime().intValue());
				break;
			case 1://+2
				time = Statistics.timeToString(cj.getSingTime().intValue());
				time += "+";
				break;
			case 2:
				time = "DNF"+" ("+time+")";
				break;

		}
		if (!TextUtils.isEmpty(cj.getEtnote())) {
			time = time + " ["+cj.getEtnote()+"]";
		}
		holder.cj.setText(time);
		holder.code.setText("["+date+"] "+code);
		holder.item_layout.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if (onClickItem != null) {
					onClickItem.onClick(position);
				}
			}
		});
		holder.btnDelete.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if (onClickItem != null) {
					onClickItem.onDelete(position);
					holder.swipe_all.quickClose();
					notifyDataSetChanged();
				}
			}
		});

	}

	@Override
	public int getItemCount() {
		return cjs==null?0:cjs.size();
	}

	public interface OnClickItem{
		void onClick(int position);
		void onDelete(int position);
	}

}
